import { Injectable } from '@nestjs/common';
import { BetService } from 'src/bet/bet.service';
import { PrismaService } from 'src/prisma/prisma.service';
import { TowerService } from 'src/tower/tower.service';
import { UpdateTowerPointDto } from './dto/update-tower-point.dto';

@Injectable()
export class TowerPointService {
  constructor(
    private prisma: PrismaService,
    private towerService: TowerService,
    private betService: BetService,
  ) {}

  async createMany(id: number) {
    //删除
    await this.prisma.pointTower.deleteMany({
      where: {
        tower: {
          lineId: id,
        },
      },
    });
    //计算
    const towers = await this.towerService.findAll({ lineId: id });
    const bets = await this.betService.findAll({ lineId: id });

    let lei = 0;
    const data = towers.map((tower, index) => {
      const y = tower.altitude * 5;
      const x = lei / 2;
      const value = {
        x,
        y,
        towerId: tower.id,
      };
      if (bets[index]) lei += bets[index].btSpan;
      return value;
    });

    return await this.prisma.pointTower.createMany({
      data,
    });
  }

  async findAll(args: Record<string, any>) {
    const { id } = args;
    return this.prisma.pointTower.findMany({
      where: { tower: { lineId: +id } },
      include: { tower: true },
    });
  }

  findOne(id: number) {
    return `This action returns a #${id} towerPoint`;
  }

  update(id: number, updateTowerPointDto: UpdateTowerPointDto) {
    return `This action updates a #${id} towerPoint`;
  }

  remove(id: number) {
    return `This action removes a #${id} towerPoint`;
  }
}
